#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

    QMessageBox msg;

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("jent");
    db.setUserName("postgres");
    db.setPassword("sucesso!@#");


    if(!db.open()){
        msg.setText("Erro");
        msg.exec();
    }else{
        QSqlQuery query;

        query = db.exec("SELECT * FROM usuario;");
        query.last();
        QString result = query.record().value(1).toString();

        QMessageBox meutexto;
        meutexto.setText(result);
        meutexto.exec();
    }

    db.close();
}

void MainWindow::on_pushButton_2_clicked()
{
    QMessageBox msg;

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("jent");
    db.setUserName("postgres");
    db.setPassword("sucesso!@#");


    if(!db.open()){
        msg.setText("Erro");
        msg.exec();
    }else{

        QSqlQuery query;

        query.exec("INSERT INTO usuario (id, sigla) VALUES (3, 'ii')");
        //query.bindValue(":sigla", 'A');
        query.exec();

        //query = db.exec("SELECT * FROM usuario;");
        //query.last();
        //QString result = query.record().value(1).toString();

        QMessageBox meutexto;
        meutexto.setText("Ok");
        meutexto.exec();
    }

    db.close();
}
